﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ufida.u9.ait.openapi.utils;
using UFIDA.U9.Base;
using UFIDA.U9.CBO.HR.Department;
using UFIDA.U9.CBO.HR.Operator;
using UFIDA.U9.CBO.SCM.PackageSV;
using UFIDA.U9.SM.DealerSO;
using UFSoft.UBF.Business;

namespace ufida.u9.ait.openapi
{
    internal class OperatorBase
    {
        public static Operators CreateDoc(CopyOfCopyOfCopyOfOperatorsDTOData dto)
        {
            Operators doc = null;
            using (ISession session = Session.Open())
            {
                doc = Operators.Create();
                doc.Org = Context.LoginOrg;
                doc.Code = dto.Code;
                UpdateEntity(dto, doc);

                session.Commit();
            }

            return doc;
        }

        public static Operators UpdateDoc(CopyOfCopyOfCopyOfOperatorsDTOData dto, Operators doc)
        {
            using (ISession session = Session.Open())
            {
                UpdateEntity(dto, doc);
                session.Commit();
            }
            return doc;
        }

        private static Operators UpdateEntity(CopyOfCopyOfCopyOfOperatorsDTOData dto, Operators doc)
        {

            doc.Name = dto.Name;
            doc.Dept = EntityUtils.GetEntityByCode<Department>(dto.Dept);//部门
            doc.Description = dto.Description;//备注
            doc.Effective=U9Utils.GetEffective(dto.Effective);//有效性

            //扩展字段
            if (dto.DescFlexField != null)
            {
                doc.DescFlexField = new UFIDA.U9.Base.FlexField.DescFlexField.DescFlexSegments();
                DTOUtils.DTOPropCopy(dto.DescFlexField, doc.DescFlexField);
            }

            //业务员行默认处理
            if (doc.OperatorLine==null|| doc.OperatorLine.Count == 0)
            {
                OperatorLine line = null;
                line = OperatorLine.Create(doc);
                line.OperatorType = OperatorTypeEnum.GetFromValue(0);//采购业务员
                line = OperatorLine.Create(doc);
                line.OperatorType = OperatorTypeEnum.GetFromValue(1);//销售业务员
                line = OperatorLine.Create(doc);
                line.OperatorType = OperatorTypeEnum.GetFromValue(2);//库管员
                line = OperatorLine.Create(doc);
                line.OperatorType = OperatorTypeEnum.GetFromValue(7);//生产管理员
                line = OperatorLine.Create(doc);
                line.OperatorType = OperatorTypeEnum.GetFromValue(12);//资产管理员
                line = OperatorLine.Create(doc);
                line.OperatorType = OperatorTypeEnum.GetFromValue(13);//其他
                line = OperatorLine.Create(doc);
                line.OperatorType = OperatorTypeEnum.GetFromValue(14);//设备管理员
            }

            return doc;
        }
    }
}
